package com.douglas.managesystem.controller.student;

import com.douglas.managesystem.common.ResponseResult;
import com.douglas.managesystem.common.RoleTypeEnum;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.controller.BaseController;
import com.douglas.managesystem.service.student.ScoreService;
import com.douglas.managesystem.vo.ScoreDTO;
import com.douglas.managesystem.vo.ScoreStuVO;
import com.douglas.managesystem.vo.UserVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-16 22:19
 **/
@Controller
@RequestMapping("/stu")
public class ScoreController extends BaseController {


    @Autowired
    private ScoreService scoreService;

    /**
     * 1.学生个人所有成绩 分页查询
     * @param model
     * @param params
     * @return
     */
    @GetMapping("/score/list")
    public String list(Model model, SearchEntity params){
            UserVO loginUser = getLoginUser();
            Map<String,Object> map = new HashMap<>();
            map.put("stuNumber",loginUser.getId());
            map.put("username",loginUser.getUsername());
            params.setEntity(map);
            PageInfo<ScoreStuVO> page = scoreService.findAllStuByPage(params);
            model.addAttribute("scoreStu",page);
            return "admin/score/list";
    }

    /**
     * 2.成绩管理列表
     * @param model
     * @param params
     * @return
     */
    @GetMapping("/score/teachList")
    public String teachList(Model model, SearchEntity params,String stuNumber){

        UserVO loginUser = getLoginUser();
        Map<String,Object> map = new HashMap<>(10);
        map.put("teacherId",loginUser.getId());
        map.put("teacherName",loginUser.getUsername());
        map.put("stuNumber",stuNumber);
        params.setEntity(map);
        PageInfo<ScoreDTO> page = scoreService.findAllScoreByPage(params);
        model.addAttribute("scoreDTOs",page);
        return "admin/score/teachList";
    }

    /**
     * 3.为编辑查询成绩
     * @param stuNumber
     * @return
     */
    @GetMapping("/score/getScore")
    @ResponseBody
    public ResponseResult toEdit(@RequestParam("stuNumber")String stuNumber){
        ResponseResult responseResult = new ResponseResult();
        try {
            ScoreDTO scoreDTO = scoreService.findScoreByStuNumber(stuNumber);
            responseResult.setInfo("成绩查询成功");
            responseResult.setSuccess(true);
            responseResult.setData(scoreDTO);
            return responseResult;
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setInfo("成绩查询失败");
            responseResult.setSuccess(false);
            return responseResult;
        }
    }


    /**
     * 4.新增或编辑
     * @param scoreDTO
     * @return
     */
    @PostMapping("/score/add")
    public String addOrEdit(ScoreDTO scoreDTO){
        //借用1枚举做判断
        if(RoleTypeEnum.TEACHER.getCode().equals(scoreDTO.getSaveOrEdit())){
            scoreService.saveScore(scoreDTO);
        }else{
            scoreService.updateScore(scoreDTO);
        }
        return "redirect:/stu/score/teachList";
    }

    /**
     * 5.删除成绩根据学号
     * @param stuNumber
     * @return
     */
    @GetMapping("/score/delete")
    public String delete(String stuNumber){
        scoreService.delete(stuNumber);
        return "redirect:/stu/score/teachList";
    }
}
